from PIL import Image
import os
import glob
import sys

path = r"H:\Deep_Learning\pix2pixHD\datasets\ciwa_quexian\train_label/*.png"
save_dir = r"H:\Deep_Learning\pix2pixHD\datasets\ciwa_qx\train_label"

file_list = [] 
write_file_name = r'ciwa.txt'
write_file = open(write_file_name, "w")
for i in glob.glob(path):
    img = Image.open(i)
    name = os.path.basename(i)
    try:
        image = img.convert("L")
        # image = Image.merge("RGB",(img,img,img))
    except:
        print(name, "  格式错误")
        file_list.append(name)
        continue
    image.save(os.path.join(save_dir, name))
    print("正在转换   ", name)
for current_line in range(len(file_list)): 
     write_file.write( '' + file_list[current_line] + '\n')
write_file.close()